package com.bus.core.channels;

import com.bus.core.events.RemoteBusEvent;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.messaging.MessageChannel;

import java.util.HashMap;
import java.util.Map;

public class BusEventChooseChannelRouter  implements ApplicationContextAware {

//    key =envent的包路径 ，value 是事件发送的channel；
    private  Map<String ,MessageChannel> channelMap;
    private ApplicationContext applicationContext;

    public BusEventChooseChannelRouter( Map<String ,MessageChannel> channelMap) {
        this.channelMap = channelMap;
    }

    MessageChannel chooseChannel(RemoteBusEvent remoteBusEvent){
        String eventName=remoteBusEvent.getName();
        return channelMap.get(eventName);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}
